package erwiki.ui.app.parts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.service.ApplicationContext;
import org.eclipse.rap.rwt.service.UISession;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;

import jakarta.annotation.PostConstruct;

public class MessagesView {

	public static final String ID = "org.elwiki.e4.ui.app.part.messagesView";

	Browser browser;
	private Text text_sessionId;

	private Text text_httpId;
	private Text textAppSession;

	// == CODE ================================================================

	/**
	 * Create contents of the view part.
	 *
	 * @param parent
	 */
	@PostConstruct
	public void createPartControl(Composite parent) {
		Composite top = new Composite(parent, SWT.NONE);
		top.setLayout(new GridLayout(2, false));

		// top banner
		Composite banner = new Composite(top, SWT.NONE);
		GridData gd_banner = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_banner.widthHint = 303;
		banner.setLayoutData(gd_banner);
		GridLayout layout = new GridLayout(1, false);
		layout.marginHeight = 5;
		layout.marginWidth = 10;
		banner.setLayout(layout);

		// setup bold font
		Font boldFont = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT);

		Label l = new Label(banner, SWT.WRAP);
		l.setText("Subject:");
		l.setFont(boldFont);
		l = new Label(banner, SWT.WRAP);
		l.setText("This is a message about the cool Eclipse RCP!");

		l = new Label(banner, SWT.WRAP);
		l.setText("From:");
		l.setFont(boldFont);

		final Link link = new Link(banner, SWT.NONE);
		link.setText("<a>nicole@mail.org</a>");
		link.addSelectionListener(new SelectionAdapter() {
			private static final long serialVersionUID = 1L;
			@Override
			public void widgetSelected(SelectionEvent e) {
				MessageDialog.openInformation(
						/*:FVK: getSite().getShell()*/null, "Not Implemented",
						"Imagine the address book or a new message being created now.");
			}
		});

		l = new Label(banner, SWT.WRAP);
		l.setText("Date:");
		l.setFont(boldFont);
		l = new Label(banner, SWT.WRAP);
		l.setText("10:34 am");
		// message contents
		Text text = new Text(banner, SWT.MULTI | SWT.WRAP);
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		text.setText(
				"This RAP Application was generated from the PDE Plug-in Project wizard. This sample shows how to:\n"
						+ "- add a top-level menu and toolbar with actions\n"
						+ "- create views that can't be closed and\n" + "  multiple instances of the same view\n"
						+ "- perspectives with placeholders for new views\n" + "- use the default about dialog\n");

		this.browser = new Browser(top, SWT.NONE);
		this.browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		Color bgColor = new Color(Display.getCurrent(), new RGB(250, 196, 250));
		//Color bgColor = new 
		this.browser.setBackground(bgColor);
		this.browser.setText("<h1>123</h1>");

		Button btnNewButton = new Button(top, SWT.NONE);
		btnNewButton.addMouseListener(new MouseAdapter() {
			private static final long serialVersionUID = -3797524270638532772L;

			@Override
			public void mouseUp(MouseEvent e) {
				testAny();
			}
		});
		btnNewButton.setText("New Button");
		new Label(top, SWT.NONE);

		Label lblNewLabel = new Label(top, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("Session ID:");

		this.text_sessionId = new Text(top, SWT.BORDER | SWT.READ_ONLY);
		this.text_sessionId.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//this.text_sessionId.setEnabled(false);

		Label lblHttpSession = new Label(top, SWT.NONE);
		lblHttpSession.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblHttpSession.setText("HTTP ID:");

		this.text_httpId = new Text(top, SWT.BORDER | SWT.READ_ONLY);
		this.text_httpId.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblNewLabel_1 = new Label(top, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText("ApplicationSession:");
		
		this.textAppSession = new Text(top, SWT.BORDER);
		this.textAppSession.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
	}

	protected void testAny() {
		// View.this.browser.setText("<h2>Привет!</h2>");
		@SuppressWarnings("unused")
		ApplicationContext ctx = RWT.getApplicationContext();

		UISession uiSessuion = RWT.getUISession();
		String uiId = uiSessuion.getId();
		this.text_sessionId.setText(uiId);

		HttpSession httpSession = uiSessuion.getHttpSession();
		String httpId = httpSession.getId();
		this.text_httpId.setText(httpId);
		
		{//:FVK:
			String resourceName = "file.css";
			String location = RWT.getResourceManager().getLocation(resourceName);
			HttpServletRequest httpRequest = RWT.getRequest();
			String url = httpRequest.getContextPath() + "/" + location;
		}
		//RWT.getRequest().getContextPath() + "/" + location;
		
		//----:FVK:
		//IApplicationSession appSession = AppUiActivator.getDefault().getAppSession();
		//this.textAppSession.setText(appSession.toString());
	}

}
